package org.example.booksmanagementsystem.controller;

import lombok.extern.slf4j.Slf4j;
import org.example.booksmanagementsystem.enums.ResultStatus;
import org.example.booksmanagementsystem.model.BookInfo;
import org.example.booksmanagementsystem.model.PageRequest;
import org.example.booksmanagementsystem.model.PageResult;
import org.example.booksmanagementsystem.model.Result;
import org.example.booksmanagementsystem.service.SpecialDealAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@Slf4j
@RequestMapping("/special")
@RestController
public class SpecialDealAdminController {
    @Autowired
    private SpecialDealAdminService specialDealAdminService;

    @RequestMapping("/getSpecialListByPage")
    public Result addSpecialBookInfo(PageRequest pageRequest, HttpSession session){
        log.info("Controller--特价秒杀展示");

        // 参数校验
        if(pageRequest.getPageSize()<1||pageRequest.getCurrentPage()<=0){

            Result result = new Result();
            result.setStatus(ResultStatus.FAIL);
            result.setErrorMessage("参数验证失败");

            return result;
        }

        PageResult<BookInfo> pageResult = null;

        try{
            pageResult = specialDealAdminService.getSpecialBookListByPage(pageRequest);
        }catch(Exception e){
            log.error("查询翻页信息错误：" + e);
        }

        return Result.success(pageResult);

    }

    // 添加图书
    @RequestMapping("/addBookInfo")
    public String addBookInfo(BookInfo bookInfo){

        //检查参数
        if(bookInfo==null
                || !StringUtils.hasLength(bookInfo.getBookName())
                || !StringUtils.hasLength(bookInfo.getAuthor())
                || !StringUtils.hasLength(bookInfo.getPublish())
                || bookInfo.getPrice() ==null
                || bookInfo.getCount()==null
                || bookInfo.getStatus()==null){
            return "输入的参数不合法，请检查后重新输入";
        }

        boolean b = true;

        try{
             b = specialDealAdminService.addBookInfo(bookInfo);
        }catch(Exception e){
            log.error("添加图书发生报错e{}："+e);
            return "添加图书失败，请联系管理员";
        }

        if(b == true)
        {
            return "";
        }else{
            return "controller 添加图书失败";
        }

    }

    @RequestMapping("/getBookInfoById")
    public BookInfo getBookInfoById(Integer bookId){
        log.info("-------bookId:"+bookId);

        if(bookId==null){
            return null;
        }

        BookInfo bookInfo = specialDealAdminService.getBookInfoById(bookId);
        if(bookInfo==null){
            return null;
        }
        log.info("--------controller---bookInfo:"+bookInfo);

        return bookInfo;
    }

    @RequestMapping("/updateSpecialBook")
    public String updateSpecialBook(BookInfo bookInfo){
        if(bookInfo==null){
            log.info("特价秒杀--update --传参为空");
            return " 错误";
        }

        boolean b = specialDealAdminService.updateSpecialBook(bookInfo);

        if(b==true){
            return "";
        }

        return "错误";
    }

    @RequestMapping("/updateBookInfo")
    public String updateBookInfo(BookInfo bookInfo){
        if(bookInfo==null){
            log.info("controller-special-单个删除传参为空");
            return "错误";
        }
        boolean b = specialDealAdminService.updateBookInfo(bookInfo);

        if(b==true){
            return "";
        }else{
            log.info("update--service返回值为false");
            return "错误";
        }

    }

    @RequestMapping("/batchDeleteBookInfoById")
    public String batchDeleteBookInfoById(@RequestParam("idList") List<Integer> idList){

        if(idList==null){
            log.info("controller--batchDelete--传来的参数为空");
            return "错误";
        }

        //调用删除单个的service
        BookInfo bookInfo = new BookInfo();
        try {
            for (Integer id : idList) {
                bookInfo.setId(id);
                bookInfo.setStatus(0);
                boolean b = specialDealAdminService.updateBookInfo(bookInfo);
                if(b==false){
                    log.info("批量删除错误");
                    return "错误";
                }
            }
        }catch (Exception e){
            log.info("批量删除抛异常");
            return "错误";
        }

        return "";
    }



}
